//
//  StatisticAreaView.swift
//  MakeMoney
//
//  Created by Fan ZhiQiang on 2024/12/20.
//

import SwiftUI

struct StatisticAreaView: View {
    @Bindable var pathStore: PathStore
    let year: Int
    let param: Int
    @State private var lotterys: [Lottery] = []
    let statisticArea: StatisticArea
    
    var filterLotterys: [Lottery] {
        switch statisticArea {
        case .front:
            return lotterys.filter { lottery in
                lottery.front1 == param || lottery.front2 == param || lottery.front3 == param || lottery.front4 == param || lottery.front5 == param
            }
        case .hind:
            return lotterys.filter { lottery in
                lottery.hind1 == param || lottery.hind2 == param
            }
        case .ball:
            return lotterys.filter { lottery in
                lottery.lotteryBall == param
            }
        }
    }
    
    
    var title: String {
        switch statisticArea {
        case .front:
            return "数字\(param)前区统计"
        case .hind:
            return "数字\(param)后区统计"
        case .ball:
            return "摇奖球\(param)统计"
        }
    }
    
    var body: some View {
        LotterysView(lotterys: filterLotterys)
            .toolbar {
                ToolbarItem(placement: .topBarTrailing) {
                    Button("更多") {
                        pathStore.path.append(StatisticMorePath())
                    }
                }
            }
            .navigationTitle(title)
            .navigationDestination(for: StatisticMorePath.self) { path in
                StatisticMore(pathStore: pathStore, lotterys: filterLotterys)
            }
            .onAppear {
                lotterys = getLotterysWith(range: getYearIdRange(with: year))
            }
    }
    
    enum StatisticArea: String {
        case front, hind, ball
    }
}

#Preview {
    StatisticAreaView(pathStore: .init(), year: 2024, param: 1, statisticArea: .front)
}

class StatisticAreaPath: PathName {
    
    var area: StatisticAreaView.StatisticArea = .front
    init(param: Int ,area: StatisticAreaView.StatisticArea) {
        self.area = area
        super.init(param: param)
    }
    
    required init(from decoder: any Decoder) throws {
        try super.init(from: decoder)
    }
    
}

